<template>
  <div class="container">
    <el-card class="box-card">
      <!-- 关键字 -->
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="关键字">
          <div class="search-wrap">
            <div class="input-item">
              <el-input placeholder="关键字" v-model="requestParameters.keyword"></el-input>
            </div>
            <div class="search-btn-wrap">
              <el-button @click="onClear">清除</el-button>
              <el-button type="primary" @click="onSearch">搜索</el-button>
            </div>
          </div>
        </el-form-item>
      </el-form>
      <!-- /关键字 -->
      <!-- 数据记录 -->
      <el-alert title="" type="info" show-icon
        >数据一共{{ total }} 条
      </el-alert>
      <!-- /数据记录 -->
      <!-- 数据列表 -->
      <el-table
        :data="randomsList"
        element-loading-text="给我一点时间"
        fit
        highlight-current-row
        style="width: 100%"
        border
      >
        <el-table-column align="center" prop="id" label="编号" min-width="200">
        </el-table-column>
        <el-table-column align="center" label="题型" min-width="80">
          <template slot-scope="scope">
            {{
              scope.row.questionType == 1
                ? "单选"
                : scope.row.questionType == 2
                ? "多选"
                : "简答"
            }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="题目编号" min-width="220">
          <template slot-scope="scope">
            <div v-for="(item, i) in scope.row.questionIDs" :key="i">
              <a
                href="javascript:;"
                class="question-id"
                @click="onPreview(item)"
                >{{ item.number }}</a
              >
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="addTime" label="录入时间" min-width="180">
        </el-table-column>
        <el-table-column
          align="center"
          prop="totalSeconds"
          label="答题时间(s)"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="accuracyRate"
          label="正确率(%)"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="userName"
          label="录入人"
          min-width="100"
        >
        </el-table-column>
        <!-- 操作按钮 -->
        <el-table-column
          align="center"
          label="操作"
          class-name="small-padding fixed-width"
          min-width="70"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click="removeRandomsId(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- /数据列表 -->

      <!-- 分页区域 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="Number(randomsList.page)"
        :page-sizes="[10,20,30,40]"
        :page-size="Number(randomsList.pagesize)"
        layout="total,prev,pager, next,sizes,jumper"
        :total="total"
      >
      </el-pagination>
      <!-- /分页区域 -->
    </el-card>

    <el-dialog
      title="题目预览"
      width="900px"
      :visible.sync="showPreview"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <RandomPreview
        v-if="showPreview"
        ref="randomPre"
        :detailId="detailId"
        :visible.sync="showPreview"
      />
    </el-dialog>
  </div>
</template>
<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions'
import RandomPreview from '../components/questions-randoms-preview'

export default {
  components: {
    RandomPreview
  },
  data () {
    return {

      requestParameters: {
        page: 1,
        pagesize: 10,
        keyword: undefined
      },
      // 获取题组列表
      randomsList: [],
      total: 0,
      showPreview: false,
      detailId: 0
    }
  },
  created () {
    this.getRandomsList()
  },
  methods: {
    async getRandomsList () {
      const { data: res } = await randoms(this.requestParameters)
      console.log(res)
      this.total = res.counts
      this.searchValue = res.keyword
      this.randomsList = res.items
      console.log('this.randomsList', this.randomsList)
    },
    // 监听 pagesize 改变的事件
    handleSizeChange (val) {
      this.requestParameters.pagesize = val
      if (this.requestParameters.page === 1) {
        this.getRandomsList(this.requestParameters)
      }
    },
    // 监听页码值改变的事件
    handleCurrentChange (val) {
      this.requestParameters.page = val
      this.getRandomsList()
    },
    onSearch () {
      this.requestParameters.page = 1
      this.getRandomsList()
    },
    onClear () {
      this.requestParameters.page = 1
      this.requestParameters.keyword = ''
      this.getRandomsList()
    },
    // 根据id删除
    removeRandomsId (id) {
      try {
        // 弹框询问是否删除
        this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            await removeRandoms({ id: id })
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            // if (this.randomsList.item.length === 1) {
            //   if (this.requestParameters.page > 1) {
            //     this.requestParameters.page--
            //   }
            // }
            this.getRandomsList()
          })
          .catch(() => {})
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    onPreview (questionIDs) {
      this.showPreview = true
      this.detailId = questionIDs.id
      console.log('detailId', this.detailId)
    }
  }
}
</script>

<style scoped lang='less'>
.container {
  /deep/.el-alert {
    .el-alert__icon.is-big {
      font-size: 16px;
      width: 16px;
    }
    .el-pagination {
      padding: 20px 5px 2px 5px;
    }
  }
  .question-id {
    color: #0099ff;
  }

  .search-wrap {
    display: flex;
    justify-content: space-between;

    .input-item {
      display: flex;
      align-items: center;
    }
  }

  .search-btn-wrap {
    display: flex;
  }
}
</style>
